/**
 * @file tiny_list.c
 * @brief 链表实现
 * @author 张恒宣 
 * @LastEditTime 2024/9/20
 * 
 * MIT License
 *
 * Copyright (c) 2025 张恒宣 luckkeymail@163.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*********************
 *      头文件
 *********************/

#include "tiny_list.h"
#include "tiny_def.h"

/*********************
*      私有函数 
*********************/

/// @brief 将节点移出链表
/// @param list_node 待移出链表的节点
static void tiny_remove_list_node(tiny_list_node_t *list_node)
{
	if (!list_node || !list_node->prev || !list_node->next)
	{
		return;
	}

	list_node->prev->next = list_node->next;
	list_node->next->prev = list_node->prev;

	tiny_init_list_node(list_node);
}

/*********************
*      公共函数 
*********************/

/// @brief 初始化循环链表节点
/// @param node 待初始化的节点
void tiny_init_list_node(tiny_list_node_t *node)
{
	node->next = node;
	node->prev = node;
	node->owner = tiny_null;
}


/// @brief 初始化循环链表
/// @param list 待初始化的循环链表
void tiny_init_list(tiny_list_t *list)
{
	if (!list)
	{
		return;
	}

	list->head = tiny_null;
	list->len = 0;
}

/// @brief 将循环链表所指向的头节点移到下一个节点
/// @param list 待操作的循环链表
void tiny_list_head_move_to_next(tiny_list_t *list)
{
	if (!list || !list->head) 
	{
		return;
	}

	list->head = list->head->next;
}

/// @brief 插入节点到另一个节点之前, 其所属链表也会被修改为目标节点所属链表
/// @param dst 目标节点
/// @param src 源节点
void tiny_insert_pre_list_node(tiny_list_node_t *dst, tiny_list_node_t *src)
{
	if (dst->owner)
	{
		src->prev = dst->prev;
		src->next = dst;
		dst->prev->next = src;
		dst->prev = src;

		src->owner = dst->owner;
		src->owner->len++;
	}
}


/// @brief 将节点插入到新链表头部
/// @param list 待插入的目标链表
/// @param node 待插入的节点
void tiny_list_insert_pre(tiny_list_t *list, tiny_list_node_t *node)
{
	if (!list || !node)
	{
		return;
	}

	if (!list->head)
	{
		list->head = node;
		list->head->next = node;
		list->head->prev = node;
		node->owner = list;
		list->len = 1;
	}
	else
	{
		tiny_insert_pre_list_node(list->head, node);
	}
}


/// @brief 将节点移除出所属链表
/// @param list_node 待移除的节点
void tiny_remove_node_from_list(tiny_list_node_t *list_node)
{
	if (!list_node)
	{
		return;
	}

	tiny_list_t *list = list_node->owner;
	if (list)
	{
		if (list_node == list->head)
		{
			tiny_list_head_move_to_next(list);
		}
		tiny_remove_list_node(list_node);
		list->len--;
		if (list->len == 0)
		{
			list->head = tiny_null;
		}
	}
}
